package com.boomsecret.pattern.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by wangjinliang on 2018/1/17.
 */
public class UserDynamicsProxy<T> implements InvocationHandler {
    private T target;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("proxy before.");
        Object result = method.invoke(target, args);
        System.out.println("proxy after");
        return result;
    }

    public T proxy(T target) {
        this.target = target;
        return (T) Proxy.newProxyInstance(UserDynamicsProxy.class.getClassLoader(), target.getClass().getInterfaces(), this);
    }
}
